

package com.bnz.service.impl;

import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.Student;
import com.bnz.entity.StudentVo;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.security.provider.certpath.CertId;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    /**
     * 查询
     * @return
     */
    @Override
    public List<Student> findAll() {
        //得到所有学生集合
        List<Student> students = studentMapper.selectByExample(null);
        System.out.println("students = " + students);
        //遍历学生集合
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //返回
        return students;
    }

    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        //开始分页
        PageHelper.startPage(page,pageSize);
        //进行查询
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();


        List<Student> students = studentMapper.selectByExample(example);
        //遍历学生集合
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //返回
        Page<Student> page1 = (Page<Student>) students;
        return new PageResult<>(page1.getTotal(),
                                        page1.getPages(),
                                        page,page1.getResult());

    }

    //条件带分页
    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVo vo) {
        //开始芬也
        PageHelper.startPage(page,pageSize);
        //查询条件
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (!StringUtils.isEmpty(vo.getSname())){
                criteria.andLike("sname","%"+vo.getSname()+"%");
            }
            if (!StringUtils.isEmpty(vo.getAddr())){
                criteria.andLike("addr","%"+vo.getAddr()+"%");
            }
            if (vo.getCid()!= null && vo.getCid() !=0){
                criteria.andEqualTo("cid",vo.getCid());
            }
        }
        //开始查询
        List<Student> students = studentMapper.selectByExample(example);
        //绑定班级
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //转化为page对象
        Page<Student> page1= (Page<Student>) students;
//        返回
        return new PageResult<>(page1.getPages(),page1.getTotal(),page,page1.getResult());
    }

    //增加
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

}
